/**
 * 报表查询条件JS
 * @author huanggang,caiyuan
 * @since 2007-11-27
 * 
 */
var dateType = "day";
var compareVisible = "";
var sortVisible = "";
		
var dateTypeCn = "";	
var fullDateFormat = "%Y-%M-%D";
var dateValueFormat = "%Y-%M-%D";
var dateShowFormat = "%Y年%M月%D日";
var dateWidth = 0;
var dateListWidth = 0;  

/**
 * 初始化日期查询条件相关参数
 */
function initDateCon(dateType)
{
	//start of 日期格式 
	if(dateType == "day")
	{
		dateTypeCn = "日";
		dateValueFormat = "%Y%M%D";
		dateShowFormat = "%Y年%M月%D日";
		dateWidth = 90;
		dateListWidth = 110;
	}
	else if(dateType == "week")
	{
		dateTypeCn = "周";
		dateValueFormat = "%Y%M%D";
		dateShowFormat = "%Y年第%W周";
		dateWidth = 75;
		dateListWidth = 160;
	}
	else if(dateType == "month")
	{
		dateTypeCn = "月";
		dateValueFormat = "%Y%M";
		dateShowFormat = "%Y年%M月";
		dateWidth = 65;
		dateListWidth = 90;
	}
	else if(dateType == "quarter")
	{
		dateTypeCn = "季";
		dateValueFormat = "%Y%Q";
		dateShowFormat = "%Y年%Q季度";
		dateWidth = 75;
		dateListWidth = 95;
	}
	else if(dateType == "year")
	{
		dateTypeCn = "年";
		dateValueFormat = "%Y";
		dateShowFormat = "%Y年";
		dateWidth = 45;
		dateListWidth = 60;
	}
	
}
 
/**
 * 按len位数, 确定高位是否要补0, 如2007-02-02
 */
function dostr(s, len) 
{
	s = s + "";
	for (var i = s.length; i < len; i++) 
	{
		s = "0" + s;
	}
	return s;
}; 
 
/**
 * 按照日期格式, 把日期类型转换成日期字符串, 如:显示型日期格式%Y年%M月%D, 日期类型2007-08-01 --> 2007年08月01日
 * @param 日期对象
 * @param 指定格式
 */
function date2str(dDate, dateFmt)
{
	var year = dDate.getYear();
	var month = dDate.getMonth() + 1;
	var quarter = (month-1)/3 +1;
	var date = dDate.getDate();
	var week = weekOfYear(dDate, 0);
	
	var datestr = dateFmt.replace(/%[Y]/, dostr(year, 4)).replace(/%[y]/, (""+year).substring(2,4)).replace(/%[M]/, dostr(month, 2)).replace(/%[D]/, dostr(date, 2)).replace(/%[Q]/, dostr(quarter, 1)).replace(/%[W]/, dostr(week, 2));
	
	return datestr;
} 

/**
 * 日期字符串转换成整型
 */
function datestr2int(datestr,dateShowFormat)
{
	var date = datestr.replace(/-+/g,"");	
	return parseInt(date);
}

/**
 * 把日期数字转换成日期字符串, 如:20070801 --> 2007-08-01, 200708 --> 2007-08
 * @param intDate 整型日期
 */
function int2datestr(intDate,dateType)
{
	var ret;
	intDate = "" + intDate;
	
	switch(dateType)
	{
		case "day":
		case "week":
			ret = intDate.substr(0,4) + "-" + intDate.substr(4,2) + "-" + intDate.substr(6,2);
			break;
		case "month":
			ret = intDate.substr(0,4) + "-" + intDate.substr(4,2);
			break;
		case "quarter":
			var intQuarter = parseInt(intDate.substr(4,1));
			ret = intDate.substr(0,4) + "-" + ((intQuarter-1)*3 +1);
			break;
		case "year":
			ret = intDate.substring(0,4);
			break;
		default:
			break;
	}
			
	return ret;
}

/**
 * 把日期字符串转换成完整的日期字符串,默认为01, 如:月时间2007-08 --> 2007-08-01; 以便进行时间的加减
 * @param datestr 日期字符串
 * @param dateType 日期类型
 */
function datestr2full(datestr, dateType)
{
	var fulldatestr;
	switch(dateType)
	{
		case "day":
		case "week":
			fulldatestr = datestr;
			break;
		case "month":
		case "quarter":
			fulldatestr = datestr + "-01";
			break;
		case "year":
			fulldatestr = datestr + "-01-01";
			break;
		default:
			break;
	}
	return fulldatestr;
}

/**
 * 日期字符串转换为指定格式的日期字符串
 */
function text2datestr(text, dateFmt)
{
	var year = month = quarter = date = week = 1;	//gh
	var v = text.split(/\W+/);
	var f = dateShowFormat.match(/%./g);
	for (var i = 0; i < f.length; i++)
	{
		if (v[i]) 
		{
			if (f[i] == "%Y") 
			{
				year = parseInt(v[i], 10);
				if (isNaN(year)) 
				{
					sl_alert("无效年份！");
					return;
				}
			} 
			else if(f[i] == "%M") 
			{
				month = parseInt(v[i], 10);
				if (isNaN(month)) 
				{
					sl_alert("无效月份！");
					return;
				}
			} 
			else if (f[i] == "%Q") 
			{	
				quarter = parseInt(v[i], 10);	//gh
				month = (quarter-1)*3 +1;	//gh
				if (isNaN(quarter)) 
				{			//gh
					sl_alert("无效季度！");
					return;
				}		
			} 
			else if (f[i] == "%D") 
			{
				date = parseInt(v[i], 10);
				if (isNaN(date)) 
				{
					sl_alert("无效日期！");
					return;
				}
			}
			else if (f[i] == "%W") 
			{
				week = parseInt(v[i], 10);
				var tempDate = dateFromWeek(year, week, 0);
				month = tempDate.getMonth() +1;
				date = tempDate.getDate();
				if (isNaN(date)) 
				{
					sl_alert("无效日期！");
					return;
				}
			} 
		}
	}
	return dateFmt.replace(/%[Y]/, dostr(year, 4)).replace(/%[M]/, dostr(month, 2)).replace(/%[D]/, dostr(date, 2)).replace(/%[Q]/, dostr(quarter, 1)).replace(/%[W]/, dostr(week, 2));
}

/**
 * 完整的日期字符串-->日期对象
 * @param fulldatestr 完整的日期字符串
 */
function fullstr2date(fulldatestr)
{	
	var miDate = Date.parse(fulldatestr.replace(/\-/g, "/"));
	var dDate = new Date(miDate);
	return dDate;
}

/**
 * 获得指定日期所在的周为第几周
 * 
 * @param dDate 日期对象
 * @param firstDayOfWeek ???
 */
function weekOfYear(dDate, firstDayOfWeek)
{ 
  var firstDateOfYear = new Date(dDate.getYear(), 0, 1);
  var offestWeek = firstDateOfYear.getDay() - firstDayOfWeek;
  var baseDate = new Date(dDate.getYear(), 0, offestWeek>=0 ? 1-offestWeek : 1-(offestWeek+7)); //alert(baseDate + "|||" + dDate);
  var weekMilli = 7*24*60*60*1000;
  var dif = dDate.getTime() - baseDate.getTime();   
  return Math.floor(dif/weekMilli)+1;
}

/**
 * 获取某年某周第一天的日期
 */
function dateFromWeek(year, week, firstDayOfWeek){
  // year   年
  // week   周
  // day    星期 (0-6, 0代表周日)
  var firstDateOfYear = new Date(year, 0, 1);
  var offestWeek = firstDateOfYear.getDay() - firstDayOfWeek;
  var baseDate = new Date(year, 0, offestWeek>=0 ? 1-offestWeek : 1-(offestWeek+7));
  //var weekMilli = 7*24*60*60*1000;
  return new Date(baseDate.getYear(), baseDate.getMonth(), baseDate.getDate() + (week-1) * 7);
 }
 
 /**
  * 日期加减
  * @param dDate 日期对象
  * @param dif 加减的数值
  * @param dateType 日期类型 日/周/月/季/年
  */
function add2date(dDate, dif, dateType)
{
	switch(dateType)
	{
		case "day":
			dDate.setDate(dDate.getDate() + dif);
			break;
		case "week":
			dDate.setDate(dDate.getDate() + dif*7);
			break;
		case "month":
			dDate.setMonth(dDate.getMonth() + dif);
			break;
		case "quarter":
			dDate.setMonth(dDate.getMonth() + dif*3);
			break;
		case "year":
			dDate.setYear(dDate.getYear() + dif);
			break;
		default:
			break;
	}
	return dDate;
}
 
/**
 * 日期比较，获得两个日期相差的天数
 * @param dStartDate 开始日期
 * @param dEndDate 结束日期
 * @param dateType 日期类型
 */
function dateCompare(dStartDate, dEndDate,dateType)
{
	var miStart = dStartDate.getTime();
	var miEnd   = dEndDate.getTime();
		
	var years = dEndDate.getYear() - dStartDate.getYear();
	var months = dEndDate.getMonth() - dStartDate.getMonth();
			
	var dayDif =  (miEnd-miStart)/(1000*24*3600);
	var dif;
	switch(dateType)
	{
		case "day":
			dif = dayDif;
			break;
		case "week":
			dif = dayDif/7;
			break;
		case "month":
			dif = years*12 + months; 
			break;
		case "quarter":
			dif = (years*12 + months)/3; 
			break;
		case "year":
			dif = years;
			break;
		default:
			break;
	}
 	return dif;
} 
 
/**
 * 获得上个对比时间（上日、上周、上月、上年）
 * @param dStartDate 对比时间
 * @param compareType 对比类型
 */
function getLastCompareDate(dStartDate, compareType)
{
	var dStart = new Date(dStartDate);
		
	switch(compareType)
	{
		case "日":
			dStart.setDate(dStart.getDate() -1);
			break;
		case "周":
			dStart.setDate(dStart.getDate() -7);
			break;
		case "月":
			dStart.setMonth(dStart.getMonth() -1);
			break;
		case "季":
			dStart.setMonth(dStart.getMonth() -3);
			break;
		case "年":
			dStart.setYear(dStart.getYear() -1);
			break;
		default:
			break;
	}
		
	return dStart;
} 
 
/* **************** 查询条件表单控件处理 ********************* */

/**
 * 双击删除列表中某时间
 * @param id 表单控件ID
 */
function removeListCurrent(id)
{
	var obj = $(id);		
	var i = obj.selectedIndex;
	if(i>=0)
	{
		obj.remove(i);
		obj.selectedIndex = 0;
	}	
}

/**
 * 清空时间列表
 * @param id 表单控件ID
 */
function clearList(id)
{		
	var obj = $(id);		
	var vOptions = obj.options;
	var length = vOptions.length;
		
	for(var i=length-1;i>=0;i--){
        vOptions.remove(i);
    }
}

/**
 * 将日期按指定格式转换
 */
function optionValue(dDate,dateValueFormat)
{
	var datestr = date2str(dDate, dateValueFormat);
	return datestr.replace(/\-/g, "");
}

/**
 * 判断时间列表中是否已经包含某时间, 对周时间只要起始日期不同即可添加
 */
function inList(optionValue, eList)
{
	var vOptions = eList.options;
		
	for(i=0; i<vOptions.length; i++)
	{
		if(vOptions[i].value == optionValue) return true;
	}
	return false;
}

/**
 *				dDate		optionText				optionValue
 *	day			2007-08-01	2007-08-01				20070801
 *	week		2007-08-01	2007-08-01--2007-08-07	20070801
 *	month		2007-08		2007-08					200708
 *	quarter		2007-07		2007年3季				200707
 *	year		2007		2007					2007
 */
function optionText(dDate, dateFmt)
{
	var oText;
	switch(dateType)
	{
		case "day":
		case "month":
		case "quarter":
		case "year":
			oText = date2str(dDate, dateFmt);
			break;
		case "week":
			var dEnd = new Date(dDate);
			dEnd.setDate(dEnd.getDate() + 6);
			oText = date2str(dDate, "%Y年第%W周") + date2str(dDate, "[%M/%D") + "-" + date2str(dEnd, "%M/%D]");;
			break;
		default:
			break;
	}
	return oText;
}
		

/**
 * 添加时间到时间列表中
 * @param dDate 日期
 * @param eList 列表对象
 * @param dateShowFormat 显示的日期样式
 */
function addList(dDate, eList,dateShowFormat)
{
	var oValue = optionValue(dDate,dateValueFormat);
	
	if(inList(oValue, eList) == false)
	{
		var vOption = document.createElement("option");		
		vOption.text = optionText(dDate, dateShowFormat);
		vOption.value = oValue;
		
		eList.add(vOption);
	}
	
}


	/**
	 * 选择对比日期类型事件
	 * @param form 表单对象
	 */
	function changeCompareType(form)
	{
		var vStartDate = $("cr.startDate").value;
		
		if(vStartDate.isEmpty())
		{
			sl_alert("请选择基准日期！");
			return;
		}
		
		var vCompareType = $("cr.compareType").value;
		
		if(vCompareType != "请选择")
		{
			var dStartDate = fullstr2date(text2datestr(vStartDate, fullDateFormat));
			
			if(vCompareType != "任意")
			{
				$("cr.randomDate").style.display = "none";
				$("randomDateAdd").style.display = "none";
				
				var dDate = getLastCompareDate(dStartDate, vCompareType);
				addList(dDate, $("cr.compareList"),dateShowFormat);
			}
			else
			{
				$("cr.randomDate").style.display = "";
				$("randomDateAdd").style.display = "";
			}				
		}
		else
		{
			$("cr.randomDate").style.display = "none";
			$("randomDateAdd").style.display = "none";
			
		}
		
	
	}
	//日期向前
	function leftDate(ele)
	{
		var vDate = ele.value;
		if(vDate == "")
		{
			return;
		}
		var dDate = fullstr2date(text2datestr(vDate, fullDateFormat));
		add2date(dDate, -1, dateType); 
		ele.value = date2str(dDate, dateValueFormat);
	}
	//日期向后
	function rightDate(ele)
	{
		var vDate = ele.value;
		if(vDate == "")
		{
			return;
		}
		var dDate = fullstr2date(text2datestr(vDate, fullDateFormat));
		add2date(dDate, 1, dateType); 
		ele.value = date2str(dDate, dateValueFormat);
	}
	//改变时间条件：简单<-->高级
	function changeConType()
	{
		if($("cr.conType").checked)
		{
			if(compareVisible != "true" && $("cr.method").value.indexOf("powerrate") == -1)
			{
				$("buttonAdd").style.display = "";
				$("cr.dateList").style.display = "";
				$("qk").style.display = "";
				clearList("cr.dateList");
				}
		}
		else
		{
			$("buttonAdd").style.display = "none";
			$("cr.dateList").style.display = "none";
			$("qk").style.display = "none";
			clearList("cr.dateList");
		}
	}
	
	
	
